package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.SysGoods;
import com.ruoyi.system.domain.SysWareHouse;
import com.ruoyi.system.domain.vo.GoodsHouseVo;
import com.ruoyi.system.service.SysGoodsService;
import com.ruoyi.system.service.SysWareHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 商品controller
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/goods/goods")
public class SysGoodsController extends BaseController {
    @Autowired
    SysGoodsService goodsService;
    @Autowired
    SysWareHouseService wareHouseService;
    /**
     * 查询商品列表
     * @return
     */
    @ApiOperation("查询列表")
    @PreAuthorize("@ss.hasPermi('goods:goods:list')")
    @PostMapping("/list")
    public TableDataInfo list(SysGoods sysGoods){
        startPage();
        List<SysGoods> list=goodsService.selectGoodsAll(sysGoods);
        return getDataTable(list);
    }
    @PostMapping("/addGoods")
    @ApiOperation("新增商品")
    @Transactional
    public AjaxResult insertGoods(@RequestBody GoodsHouseVo goodsHouseVo) {
        int rs = goodsService.insertGoods(goodsHouseVo.getGoods());
        if (rs > 0) {
            rs = wareHouseService.insertWareHouse(goodsHouseVo.getHouse());
            return rs > 0 ? success("插入成功") : error("插入失败");
        }
        return error("插入失败");
    }
    @GetMapping("/load")
    @ApiOperation("商品导出")
    public String load(HttpServletRequest request){
       return goodsService.land(request);
    }
}
